﻿using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Text;

namespace ReaWTM.LayuiHelper
{
	[HtmlTargetElement("wt:print", TagStructure = TagStructure.NormalOrSelfClosing)]
	public class PrintTagHelper : BaseTag
	{
		public override void Process(TagHelperContext context, TagHelperOutput output)
		{
			output.TagName = "print";
			output.TagMode = TagMode.StartTagAndEndTag;

			if (WtSize != WtSizeEnum.None)
			{
				output.Attributes.Add("wtsize", base.GetWtSize());
			}
			context.Items.Add("wt-print", null);

			var mark = $"print_{DateTime.Now.Ticks}";
			output.Attributes.Add(mark, null);
			StringBuilder pre = new StringBuilder();
			StringBuilder post = new StringBuilder();
			pre.Append($"<button class='layui-btn layui-btn-normal layui-btn-sm print-btn no-print'>打印</button>");

			post.Append($@"
<script>
try{{
wtm.print_Render('[{mark}]');
}}catch{{}}
</script>
");
			output.PreContent.AppendHtml(pre.ToString());
			output.PostContent.AppendHtml(post.ToString());

		}
	}
}
